package com.sport.app.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sport.app.R;

public class ProgressPopup extends AlertDialog {
    private Context mContext;

    private TextView mMessageView;

    private CharSequence mMessage;
    private ProgressBar progressBar;
    private boolean mHasStarted;
    private Handler mViewUpdateHandler;

    public ProgressPopup(Context context) {
//        this(context, R.style.AppTheme.Popup.Progress);
        this(context, 1);
    }

    public ProgressPopup(Context context, int theme) {
        super(context, theme);
        mContext = context;
    }

    public static ProgressPopup show(Context context,
                                     CharSequence message) {
        return show(context, message, false);
    }

    public static ProgressPopup show(Context context,
                                     CharSequence message, boolean cancelable) {
        return show(context, message, cancelable, null);
    }

    public static ProgressPopup show(Context context, CharSequence message,
                                     boolean cancelable, OnCancelListener cancelListener) {
        ProgressPopup dialog = new ProgressPopup(context);
        dialog.setMessage(message);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.show();
        return dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.progress_popup, null);
        progressBar = (ProgressBar) view.findViewById(R.id.progress);
//        Circle circle = new Circle();
//        circle.setBounds(0, 0, 100, 100);
//        circle.setColor(getContext().getResources().getColor(R.color.progress_color));
//        progressBar.setIndeterminateDrawable(circle);
        mMessageView = (TextView) view.findViewById(R.id.message);
        setView(view);

        setMessage(mMessage);

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        mHasStarted = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHasStarted = false;
    }

    @Override
    public void setMessage(CharSequence message) {
        if (mMessageView != null) {
            if (TextUtils.isEmpty(message)) {
                mMessageView.setVisibility(View.GONE);
            } else {
                mMessageView.setVisibility(View.VISIBLE);
                mMessageView.setText(message);
            }
        } else {
            mMessage = message;
        }
    }
}
